<?php

/**
 * Activity Logging class used to add and log a user's activity to the database.
 * @author Cristian Asenjo on 04/12/2011
 */

class ActivityLogger {
    
    // Partial log query
    private $log_query;
    
    // Count query to figure out how many log entries we have so far for the user in question
    private $count_query;
    
    // Order query to get the oldest entry
    private $order_query;
    
    // How to order the counted results
    private $orderBy;
    
    // Update query for when we're replacing an older entry
    private $update_query_start;
    private $update_query_end;
    
    // Maximum number of log entries allowed before we start overwriting the oldest one
    private $MAX_LOG_COUNT = 10;
    
    // Update the user's last login time
    private $update_login_query;
    
    //Constructor
    function __construct($TABLE_LOGS, $TABLE_LOGS_LOGGEDAT, $TABLE_LOGS_USERID, $TABLE_LOGS_ACTIVITYTYPE, $TABLE_LOGS_EXTRACOMMENTS, $TABLE_LOGS_ID) {
        
        // Initialize the log query
        $this->log_query = "INSERT INTO $TABLE_LOGS 
        ($TABLE_LOGS_LOGGEDAT, $TABLE_LOGS_USERID, $TABLE_LOGS_ACTIVITYTYPE, $TABLE_LOGS_EXTRACOMMENTS) 
        VALUES (".date("U")." ,";
        
        // Initialize the counting query
        $this->count_query = "SELECT COUNT(*) FROM $TABLE_LOGS WHERE $TABLE_LOGS_USERID = ";
        
        // Initialize the ordered query
        $this->order_query = "SELECT * FROM $TABLE_LOGS WHERE $TABLE_LOGS_USERID = ";
        
        // Initialize the orderBy clause
        $this->orderBy = " ORDER BY $TABLE_LOGS_LOGGEDAT ASC";
        
        // Initialize the update queries
        $this->update_query_start = "UPDATE $TABLE_LOGS SET $TABLE_LOGS_LOGGEDAT = ".date("U").",
                                $TABLE_LOGS_EXTRACOMMENTS = '";
        
        $this->update_query_end = "' WHERE $TABLE_LOGS_ID = ";
    }
    
    /**
     * Logs a the user's last logging in/out time
     * @param $userID           Integer  The <b>internal</b> userID used by the system to log the user in
     * @param $activityType     Integer  The type of activity
     * @param $comments         String   Any extra relevant comments (i.e. IP address)
     */    
    function addLogging($userID, $activityType, $comments) {
        
        // Count how many entries we already have
        $resultSet = mysql_query($this->count_query.$userID);
        $result = mysql_fetch_array($resultSet);
        
        // If there's less than the max allowed
        if( $result[0] < $this->MAX_LOG_COUNT) {
            
            // Complete the log query
            $this->log_query .= $userID.", ".$activityType.", '".$comments."')";
            
            // Log it
            mysql_query($this->log_query);
        }
        else {
            // Complete the order query
            $this->order_query .= $userID.$this->orderBy;
            
            // Get the oldest entry
            $resultSet = mysql_query($this->order_query);
            $result = mysql_fetch_array($resultSet);

            // Complete the update query, where result[0] is the entry's ID in the previous result set
            $this->update_query_start .= $comments.$this->update_query_end.$result[0];
            mysql_query($this->update_query_start);
        }
    }

    /**
     * Updates the user's last login time to the user table
     * @param $userID  Integer  The <b>internal</b> userID used by the system to log the user in
     * @param $TABLE_USERS  String    The name of the users table
     * @param $TABLE_USERS_USERID  String   The name of the userID column
     * @param $TABLE_USERS_LASTCONNECTED  String  The name of the lastConnected column
     */
    function updateLastConnected($userID, $TABLE_USERS, $TABLE_USERS_USERID, $TABLE_USERS_LASTCONNECTED) {
        
        // Build the query
        $this->update_login_query = "UPDATE $TABLE_USERS 
                SET $TABLE_USERS_LASTCONNECTED = ".date("U").
                " WHERE $TABLE_USERS_USERID = $userID";
        
        // Execute it
        mysql_query($this->update_login_query);        
    }
    
}